/**
 *  Copyright (c) 2014 The xmlepf project.  All rights reserved.
 *
 *  This file is part of xmlepf (Event Driven Testing Framework).
 *
 *  xmlepf is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  xmlepf is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with xmlepf.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This software consists of voluntary contributions made by many
 *  individuals.  For exact contribution history, see the revision
 *  history and logs, available at http://code.google.com/p/xmlepf.
 */
package org.xmlepf.xtemp.eventboard;

import java.util.LinkedList;
import java.util.List;
import org.xmlepf.utils.ObjectCreator;

/**
 *
 *
 */
public class EventBoardManager {

    private static List<EventBoard> boardList;
    private static EventBoardFactory boardFactory;

    public EventBoardManager() throws Exception {
        boardList = new LinkedList<EventBoard>();
        boardFactory = (EventBoardFactory) ObjectCreator.get("EventBoardFactory");
    }
    
    public void create(List<String> boardsXml) throws Exception {
        for (String boardXml : boardsXml) {
            create(boardXml);
        }
    }

    public void create(String boardXml) throws Exception {
        EventBoard board = boardFactory.create(boardXml);
        boardList.add(board);
    }
    
    public void remove(String boardName) throws Exception {
        for (EventBoard board : boardList) {
            EventBoardInfo boardInfo = board.getInfo();
            if (boardInfo.getName().compareTo(boardName) == 0) {
                boardList.remove(board);
            }
        }
        throw new Exception("Event board \"" + boardName + "\" was not found.");
    }

    public void removeAll() throws Exception {
        for (EventBoard board : boardList) {
            boardList.remove(board);
            board = null;
        }
    }

    public boolean hasEventBoard(String boardName) {
        for (EventBoard board : boardList) {
            EventBoardInfo boardInfo = board.getInfo();
            if (boardInfo.getName().compareTo(boardName) == 0) {
                return true;
            }
        }
        return false;
    }

    public EventBoard getEventBoard(String boardName) throws Exception {
        for (EventBoard board : boardList) {
            EventBoardInfo boardInfo = board.getInfo();
            if (boardInfo.getName().compareTo(boardName) == 0) {
                return board;
            }
        }

        throw new Exception("Event board \"" + boardName + "\" was not found.");
    }

    public List<EventBoard> getEventBoards() throws Exception {
        return boardList;
    }

    public List<EventBoardInfo> getEventBoardsInfo() throws Exception {
        List<EventBoardInfo> boardInfoList = new LinkedList<EventBoardInfo>();
        for (EventBoard board : boardList) {
            boardInfoList.add(board.getInfo());
        }
        return boardInfoList;
    }
}
